import { ToastController } from 'ionic-angular/components/toast/toast-controller';
import { ViewController } from 'ionic-angular/navigation/view-controller';
import { Component } from '@angular/core';
import { NavController, NavParams } from 'ionic-angular';
import { BaseUI } from '../../common/baseui';
import { LoadingController } from 'ionic-angular/components/loading/loading-controller';
import { RestProvider } from '../../providers/rest/rest';

/**
 * Generated class for the RegisterPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */


@Component({
  selector: 'page-register',
  templateUrl: 'register.html',
})
export class RegisterPage extends BaseUI{

  mobile: any
  nickname: any
  password: any
  confirmPassword: any
  errorMessage: any

  constructor(public navCtrl: NavController,
              public navParams: NavParams,
              public viewCtrl: ViewController,
              public loadingCtrl: LoadingController,
              public toastCtrl: ToastController,
              public rest: RestProvider
            ) {
              super()
  }

  dismiss() {
    this.viewCtrl.dismiss()
  }

  gotoLogin() {
    this.navCtrl.pop()
  }

  doRegister() {
    // 前台验证表单数据的正确性
    if(!(/^1[34578]\d{9}$/.test(this.mobile))) {
      super.showToast(this.toastCtrl,"您的手机号码格式不确认")      
    } else if(this.nickname.length < 3 
              || this.nickname.length > 10 
              || this.password.length < 6 
              || this.password.length > 20){
      super.showToast(this.toastCtrl,"昵称3~10位之间，密码长度6~20位之间")
    }else if(this.password != this.confirmPassword) {
      super.showToast(this.toastCtrl,"两次输入的密码不匹配")
    } else {
      let loading = super.showLoading(this.loadingCtrl, '注册中')
      this.rest.register(this.mobile, this.nickname, this.password)
      .subscribe(
        f => {
          if(f["Status"] == "OK"){
            loading.dismiss()
            this.dismiss()
          } else {
            loading.dismiss()
            super.showToast(this.toastCtrl, f["StatusContent"])
          }
          error => this.errorMessage = <any>error
        }
      )
    }
    
  }
}
